/*
* @Author: Ziwei Yang
* @Date:   2016-10-14
* @Last Modified by:   Ziwei Yang
* @Last Modified time: 2016-10-19
*/

'use strict';

document.onkeydown = function(event){
	var direction = null;
	switch(event.keyCode){
		case 68:
			direction = 'right';
			socket.emit('keyPress',{inputId:'right',state:false});
			socket.emit('keyPress',{inputId:'down',state:false});
			socket.emit('keyPress',{inputId:'left',state:false});
			socket.emit('keyPress',{inputId:'up',state:false});
			break;
		case 83:
			direction = 'down';
			socket.emit('keyPress',{inputId:'right',state:false});
			socket.emit('keyPress',{inputId:'down',state:false});
			socket.emit('keyPress',{inputId:'left',state:false});
			socket.emit('keyPress',{inputId:'up',state:false});
			break;
		case 65:
			direction = 'left';
			socket.emit('keyPress',{inputId:'right',state:false});
			socket.emit('keyPress',{inputId:'down',state:false});
			socket.emit('keyPress',{inputId:'left',state:false});
			socket.emit('keyPress',{inputId:'up',state:false});
			break;
		case 87:
			direction = 'up';
			socket.emit('keyPress',{inputId:'right',state:false});
			socket.emit('keyPress',{inputId:'down',state:false});
			socket.emit('keyPress',{inputId:'left',state:false});
			socket.emit('keyPress',{inputId:'up',state:false});
			break;
	}
	if(direction){
		socket.emit('keyPress',{inputId: direction,state:true});
	}
	// else if(attack){
	// 	socket.emit('keyPress',{inputId: attack,state:true});
	// }
}


// document.onkeydown = function(event){
// 	//event.preventDefault();
// 	if(event.keyCode === 68) //d
// 		socket.emit('keyPress',{inputId:'right',state:true});
// 	else if(event.keyCode === 83) //s
// 		socket.emit('keyPress',{inputId:'down',state:true});
// 	else if(event.keyCode === 65) //a
// 		socket.emit('keyPress',{inputId:'left',state:true});
// 	else if(event.keyCode === 87) //w
// 		socket.emit('keyPress',{inputId:'up',state:true});
// }
document.onkeyup = function(event){
	//event.preventDefault();
	if(event.keyCode === 68) //d
		socket.emit('keyPress',{inputId:'right',state:false});
	else if(event.keyCode === 83) //s
		socket.emit('keyPress',{inputId:'down',state:false});
	else if(event.keyCode === 65) //a
		socket.emit('keyPress',{inputId:'left',state:false});
	else if(event.keyCode === 87) //w
		socket.emit('keyPress',{inputId:'up',state:false});
	if(event.keyCode === 32){
		socket.emit('keyPress',{inputId:'attack',state:true});
		//socket.emit('keyPress',{inputId:'attack',state:false});
	}
		
}

var drawMap = function(){
	ctx.clearRect(0,0,500,500);
		for(var i in map.data){
			for(var k in map.data[i]){
				var target = map.data[i][k];
				if(target.defined){
					var data = target.defined;
					if(data.name === "black"){
						ctx.fillStyle = 'black';
						ctx.fillRect(target.x,target.y,target.width,target.height);	
					}else{
						ctx.drawImage(img,data.x,data.y,16,16,target.x,target.y,25,25);
				}
			}
		}
	}
}
